package programmercarl.贪心.C22;

class Solution {
    //从后往前遍历
    public int monotoneIncreasingDigits(int n) {
        String[] str = Integer.toString(n).split("");
        int start = str.length - 1;
        for (int i = str.length - 1; i > 0; i--) {
            if (Integer.parseInt(str[i]) < Integer.parseInt(str[i - 1])) {
                str[i] = (Integer.parseInt(str[i - 1]) -1)+"";
                start = i;
            }
        }
        for (int i = start; i < str.length; i++) {
            str[i] = "9";
        }
        return Integer.parseInt(String.join("", str));
    }
}